global without sharing class MobileAppRequestSearchExtension
{
    public List<Request__c> companyRequests {get;set;}
    public String requestNumberFromQueryString{get;set;}
    
    public MobileAppRequestSearchExtension()
    {
    }
    
    public MobileAppRequestSearchExtension(ApexPages.StandardController controller) 
    {
        requestNumberFromQueryString = ApexPages.currentPage().getParameters().get('requestNumber');
        
        List<Contact> contactList = new List<Contact> 
                                            ([SELECT Id, Name, Current_Employer__c 
                                             FROM Contact 
                                             WHERE Id IN (Select ContactId FROM User WHERE Id = :UserInfo.getUserId())
                                             LIMIT 1]);
        
        Contact currentContact = null;
        
        if (contactList.size() > 0)
            currentContact = contactList[0];
        else
            currentContact = new Contact();
        
        companyRequests = new List<Request__c>([SELECT Id, Name, Public_Status__c
                                                FROM Request__c
                                                WHERE Company__c = :currentContact.Current_Employer__c]);
    }
    
    @RemoteAction
    global static RequestSearchResult searchRequests (String requestNumber) 
    {
        requestNumber = requestNumber.replaceFirst('^(\\D*)', 'REQ-');
        
        List<Request__c> resultList = new List<Request__c>([SELECT Id, Name, Public_Status__c, 
                                                            Transaction_Type__r.Name, Company__r.Name, 
                                                            Company__r.License_Number_Formula__c, 
                                                            CreatedBy.Name
                                                            FROM Request__c
                                                            WHERE Name = :requestNumber]);
        
        RequestSearchResult returnRequestResultObject = null;
        
        if(resultList.size() > 0)
        {
            returnRequestResultObject = new RequestSearchResult();
            returnRequestResultObject.Id = resultList[0].Id;
            returnRequestResultObject.name = resultList[0].Name;
            returnRequestResultObject.publicStatus = resultList[0].Public_Status__c;
            returnRequestResultObject.transactionTypeName = resultList[0].Transaction_Type__r.Name;
            returnRequestResultObject.companyName = resultList[0].Company__r.Name;
            returnRequestResultObject.companyLicenseNumber = resultList[0].Company__r.License_Number_Formula__c;
            returnRequestResultObject.createdBy = resultList[0].CreatedBy.Name;
        }
        
        
        return returnRequestResultObject;
    }
    
    global class RequestSearchResult
    {
        public String Id;
        public String name;
        public String publicStatus;
        public String transactionTypeName;
        public String companyName;
        public Decimal companyLicenseNumber;
        public String createdBy;
    }
}